package idv.loveloser.servlet;

import idv.loveloser.dao.MdlCourse;
import idv.loveloser.dao.MdlCourseDAO;
import idv.loveloser.dao.MdlCourseSections;
import idv.loveloser.dao.MdlCourseSectionsDAO;
import idv.loveloser.dao.MdlRoleAssignmentsDAO;
import idv.loveloser.util.Util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

public class AddCourse extends HttpServlet {
	private MdlCourseDAO courseDAO=new MdlCourseDAO();
	private MdlRoleAssignmentsDAO roleAssignmentDAssignmentsDAO = new MdlRoleAssignmentsDAO();
	private MdlCourseSectionsDAO courseSectionDAO = new MdlCourseSectionsDAO();
	/**
	 * Constructor of the object.
	 */
	public AddCourse() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		//response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		String client = request.getParameter("client");
		String sessionKey = request.getParameter("sessionKey");
		String permitionToCreateCourse="moodle/course:create";
		long userId=-0L;
		long roleId=-1L;
		if(Util.isAuthorised(client, sessionKey)){
			userId = Util.getUserId(Long.parseLong(client));
			System.out.println("[UserId]:"+userId);
			roleId = Util.getRoleId(userId);
			System.out.println("[roleId]:"+roleId);
//			if(!Util.hasCapability(permitionToCreateCourse, roleId)){
//				System.out.println("[hasCapability]:false");
//				JsonObject json = new JsonObject();
//				json.addProperty("add_course", false);
//				out.print(json.toString());
////				return;
//			}else{
				System.out.println("[hasCapability]:true");
				
				String category = request.getParameter("category");
				String fullname=request.getParameter("fullname");
				String shortname=request.getParameter("shortname");
				String idnumber = System.currentTimeMillis()+"";
				MdlCourse course = new MdlCourse();
				course.setCategory(Long.parseLong(category));
				course.setFullname(fullname);
				course.setShortname(shortname);
				course.setIdnumber(idnumber);
				course.setCompletionstartonenrol(false);
				course.setEnablecompletion(false);
				course.setId(Long.parseLong(idnumber));
				course.setSortorder(10001L);
				course.setVisible(true);
				course.setSummaryformat((short)0);
				course.setFormat("weeks");
				course.setShowgrades((short)1);
				course.setNewsitems(5);
				course.setStartdate(System.currentTimeMillis());
				course.setNumsections(1);
				course.setMarker(0L);
				course.setMaxbytes(0L);
				course.setLegacyfiles((short)0);
				course.setShowreports((short)0);
				course.setHiddensections((short)0);
				course.setGroupmode((short)0);
				course.setGroupmodeforce((short)0);
				course.setLang("");
				course.setTheme("");
				course.setTimecreated(System.currentTimeMillis());
				course.setTimemodified(System.currentTimeMillis());
				course.setRequested(false);
				course.setRestrictmodules(false);
				course.setVisibleold(true);
				course.setDefaultgroupingid(0L);
				course.setCompletionnotify(false);
				course.setModinfo("a:8:{i:2;O:8:\"stdClass\":11:{s:2:\"id\";s:1:\"1\";s:2:\"cm\";s:1:\"2\";s:3:\"mod\";s:4:\"chat\";s:7:\"section\";s:1:\"0\";s:9:\"sectionid\";s:1:\"2\";s:6:\"module\";s:1:\"2\";s:5:\"added\";s:10:\"1329743432\";s:7:\"visible\";s:1:\"1\";s:10:\"visibleold\";s:1:\"1\";s:16:\"showavailability\";s:1:\"1\";s:4:\"name\";s:15:\"測試聊天室\";}i:3;O:8:\"stdClass\":12:{s:2:\"id\";s:1:\"2\";s:2:\"cm\";s:1:\"3\";s:3:\"mod\";s:5:\"forum\";s:7:\"section\";s:1:\"0\";s:9:\"sectionid\";s:1:\"2\";s:6:\"module\";s:1:\"7\";s:5:\"added\";s:10:\"1339655014\";s:7:\"visible\";s:1:\"1\";s:10:\"visibleold\";s:1:\"1\";s:9:\"groupmode\";s:1:\"2\";s:16:\"showavailability\";s:1:\"1\";s:4:\"name\";s:21:\"測試使用討論區\";}i:4;O:8:\"stdClass\":11:{s:2:\"id\";s:1:\"1\";s:2:\"cm\";s:1:\"4\";s:3:\"mod\";s:10:\"assignment\";s:7:\"section\";s:1:\"0\";s:9:\"sectionid\";s:1:\"2\";s:6:\"module\";s:1:\"1\";s:5:\"added\";s:10:\"1339655043\";s:7:\"visible\";s:1:\"1\";s:10:\"visibleold\";s:1:\"1\";s:16:\"showavailability\";s:1:\"1\";s:4:\"name\";s:12:\"檔案上傳\";}i:5;O:8:\"stdClass\":11:{s:2:\"id\";s:1:\"2\";s:2:\"cm\";s:1:\"5\";s:3:\"mod\";s:10:\"assignment\";s:7:\"section\";s:1:\"0\";s:9:\"sectionid\";s:1:\"2\";s:6:\"module\";s:1:\"1\";s:5:\"added\";s:10:\"1339655066\";s:7:\"visible\";s:1:\"1\";s:10:\"visibleold\";s:1:\"1\";s:16:\"showavailability\";s:1:\"1\";s:4:\"name\";s:12:\"線上文字\";}i:6;O:8:\"stdClass\":11:{s:2:\"id\";s:1:\"3\";s:2:\"cm\";s:1:\"6\";s:3:\"mod\";s:10:\"assignment\";s:7:\"section\";s:1:\"0\";s:9:\"sectionid\";s:1:\"2\";s:6:\"module\";s:1:\"1\";s:5:\"added\";s:10:\"1339655088\";s:7:\"visible\";s:1:\"1\";s:10:\"visibleold\";s:1:\"1\";s:16:\"showavailability\";s:1:\"1\";s:4:\"name\";s:18:\"上傳一個檔案\";}i:7;O:8:\"stdClass\":11:{s:2:\"id\";s:1:\"4\";s:2:\"cm\";s:1:\"7\";s:3:\"mod\";s:10:\"assignment\";s:7:\"section\";s:1:\"0\";s:9:\"sectionid\";s:1:\"2\";s:6:\"module\";s:1:\"1\";s:5:\"added\";s:10:\"1339655107\";s:7:\"visible\";s:1:\"1\";s:10:\"visibleold\";s:1:\"1\";s:16:\"showavailability\";s:1:\"1\";s:4:\"name\";s:12:\"離線作業\";}i:8;O:8:\"stdClass\":10:{s:2:\"id\";s:1:\"3\";s:2:\"cm\";s:1:\"8\";s:3:\"mod\";s:5:\"forum\";s:7:\"section\";s:1:\"0\";s:9:\"sectionid\";s:1:\"2\";s:6:\"module\";s:1:\"7\";s:5:\"added\";s:10:\"1340029219\";s:7:\"visible\";s:1:\"1\";s:10:\"visibleold\";s:1:\"1\";s:4:\"name\";s:12:\"最新消息\";}i:9;O:8:\"stdClass\":13:{s:2:\"id\";s:1:\"1\";s:2:\"cm\";s:1:\"9\";s:3:\"mod\";s:4:\"wiki\";s:7:\"section\";s:1:\"0\";s:9:\"sectionid\";s:1:\"2\";s:6:\"module\";s:2:\"19\";s:5:\"added\";s:10:\"1340036070\";s:7:\"visible\";s:1:\"1\";s:10:\"visibleold\";s:1:\"1\";s:16:\"showavailability\";s:1:\"1\";s:15:\"showdescription\";s:1:\"1\";s:7:\"content\";s:61:\"<div class=\"no-overflow\"><p>新增一個新的 Wiki</p></div>\";s:4:\"name\";s:23:\"新增一個新的 Wiki\";}}");
				courseDAO.save(course);
				for(int index=0;index<1;index++){
					MdlCourseSections section = new MdlCourseSections();
					section.setCourse(course.getId());
					section.setVisible(true);
					section.setSection((long)index);
					section.setSequence(index+"");
					section.setSummaryformat((short)1);
					courseSectionDAO.save(section);
				}
				Gson gson = new Gson();
				out.println(gson.toJson(course));
				
//			}
		}

		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
